package com.jy.modules.cims.loan.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.jy.modules.cims.common.CimsConstant;
import com.jy.modules.cims.common.exception.CimsExceptionConstant;
import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.component.common.service.TCMmTransConfigService;
import com.jy.modules.cims.data.common.dao.TCMmCreditDetailDao;
import com.jy.modules.cims.data.common.dto.TCMmCooperorgDealDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDealDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmCreditDetailDTO;
import com.jy.modules.cims.data.common.dto.TCMmTransConfigDTO;
import com.jy.modules.cims.data.dao.DealDao;
import com.jy.modules.cims.data.dto.CreditDetailDto;
import com.jy.modules.cims.data.dto.credit.RedeemParamDto;
import com.jy.modules.cims.data.dto.credit.RedeemResultDto;
import com.jy.modules.cims.data.dto.loan.LoanTransferCrossBillDateDto;
import com.jy.modules.cims.deal.service.DealService;
import com.jy.modules.cims.interact.ICInteract;
import com.jy.modules.cims.interact.IInteractAssist;
import com.jy.modules.cims.transexception.service.TransExceptionService;

/**
 * @description 贷款回购业务服务层
 * @author yanjingying
 * @date 2015-12-23
 */
@Service("com.jy.modules.cims.loan.service.LoanRedeemService")
public class LoanRedeemService {
	private static final Logger logger = LoggerFactory.getLogger(LoanRedeemService.class);
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CInteractAssist")
	public IInteractAssist iInteractAssist;
	
	public LoanRedeemService(){
	}
	
	/**
	 * 交易处理服务层
	 */
	@Autowired
	public DealDao dealDao;
	
	@Autowired
	@Qualifier("com.jy.modules.cims.interact.CCInteractImpl")
	private ICInteract iCInteract;

	/**
	 * 系统交互处理服务层
	 */
	@Autowired
	@Qualifier("com.jy.modules.cims.deal.service.DealService")
	public DealService dealService;
	
	@Autowired
    public TCMmCreditDetailDao tCMmCreditDetailDao;
	
    @Autowired
	@Qualifier("com.jy.modules.cims.component.common.service.TCMmTransConfigService")
    private TCMmTransConfigService transConfigService;
    
    @Autowired
	@Qualifier("com.jy.modules.cims.transexception.service.TransExceptionService")
    private TransExceptionService transExceptionService;
    
    /**
     * 获取贷款回购跨账单日结果列表
     * @throws Exception 
     * @Title getLoanTransferCrossBillDate
     * @Description TODO
     * @return 
     * @author yanjingying
     * @date 2015-12-29-下午18:47:00
     * @update
     * @throws
     * */
    public List<LoanTransferCrossBillDateDto> getLoanTransferCrossBillDate(TCMmCooperorgDealDTO cooperorgDealDTO) throws Exception{
    	List<LoanTransferCrossBillDateDto> loanTransferCrossBillDateDtoList = new ArrayList<LoanTransferCrossBillDateDto>();
    	Map<String, Object> searchParams = new HashMap<String, Object>();
		TCMmCreditDealDetailDTO creditDealDetailDTO = new TCMmCreditDealDetailDTO();
		creditDealDetailDTO.setDealStreamCode(cooperorgDealDTO.getDealCode());    //交易流水号
		searchParams.put("dto", creditDealDetailDTO);
		List<TCMmCreditDealDetailDTO> creditDealDetailDTOList  = dealService.getDealDetailByCreditInfo(searchParams);
		for(TCMmCreditDealDetailDTO dto : creditDealDetailDTOList){
			Date redeemDate = dto.getRedeemDate();   //赎回日期
			Long billDate = dto.getBillDate();       //账单日
			Date nextBillDate = null;                //下一账单日
			Date redeemConfirmDate = new Date();           //赎回确认日期
			String redeemDateYearMonth = DateTimeUtils.formatDate(redeemDate, DateTimeUtils.DEFAULT_DATE_TIME_FORMAT).substring(0, 7);   //赎回日期的年月
			int redeemDateDay = Integer.parseInt(DateTimeUtils.formatDate(redeemDate, DateTimeUtils.DEFAULT_DATE_TIME_FORMAT).substring(8, 10));   //赎回日期的天
			String zeroPot = " 00:00:00"; 
			//System.out.println("================redeemDateYearMonth:"+ redeemDateYearMonth + "===================redeemDateDay"+redeemDateDay);
			
			//获取下一账单日,业务规则如下：
			//若账单日为1，无论赎回日期的日期是多少，下一账单日均为(月份+1)+账单日
			if(new Long(1).equals(billDate)){
				nextBillDate = DateTimeUtils.parseDate((DateTimeUtils.getLastMonth((redeemDateYearMonth),0,1,0)  + "-" +  billDate + zeroPot), DateTimeUtils.DEFAULT_DATE_TIME_FORMAT);   //下一账单日=下一月份+账单日
			}
			
			/*若账单日为16
			 * 如果赎回日期的日期是1~15，则下一账单日为(月份)+账单日;
			 * 如果赎回日期的日期是16~31，则下一账单日为(月份+1)+账单日
			 * */
			if(new Long(16).equals(billDate)){
				if(redeemDateDay >= 16 && redeemDateDay <= 31){
					nextBillDate = DateTimeUtils.parseDate((DateTimeUtils.getLastMonth((redeemDateYearMonth),0,1,0)  + "-" +  billDate + zeroPot), DateTimeUtils.DEFAULT_DATE_TIME_FORMAT);   //下一账单日=下一月份+账单日
				}else if(redeemDateDay >= 1 && redeemDateDay <= 15){
					nextBillDate = DateTimeUtils.parseDate((DateTimeUtils.getLastMonth((redeemDateYearMonth),0,0,0)  + "-" +  billDate + zeroPot), DateTimeUtils.DEFAULT_DATE_TIME_FORMAT);   //下一账单日=月份+账单日
				}else{
				}
			}
			
			//如果赎回确认日期大于等于下一账单日，则跨账单日，需要给予提示
			if(DateTimeUtils.diffDays(redeemConfirmDate, nextBillDate)>=0){
				LoanTransferCrossBillDateDto loanTransferCrossBillDateDto = new LoanTransferCrossBillDateDto();
				loanTransferCrossBillDateDto.setDealStreamCode(cooperorgDealDTO.getDealCode());
				loanTransferCrossBillDateDto.setTransferId(dto.getTransferId());         //债权转让编号
				loanTransferCrossBillDateDto.setCreditorId(dto.getCreditorId());         //债权编号
				loanTransferCrossBillDateDto.setBillDate(billDate);                      //账单日
				loanTransferCrossBillDateDto.setNextBillDate(nextBillDate);              //下一账单日
				loanTransferCrossBillDateDto.setRedeemDate(redeemDate);                  //赎回日期
				loanTransferCrossBillDateDto.setRedeemConfirmDate(redeemConfirmDate);    //赎回确认日期
				loanTransferCrossBillDateDto.setIsCrossBillDate(CimsConstant.COOPERORG_IS_CROSS_BILLDATE_YES);   //是否跨账单日 1-是 0-否
				loanTransferCrossBillDateDtoList.add(loanTransferCrossBillDateDto);
			}
		}
    	return loanTransferCrossBillDateDtoList;
    }
	
	/**
	 * 贷款转出批量回购事务正常及异常处理
     * @throws Exception 
     * @Title doLoanTransferRedeemA
     * @Description TODO
     * @return 
     * @author yanjingying
     * @date 2015-12-23-下午11:47:00
     * @update
     * @throws
	 * */
	public Map<String, String> doLoanTransferRedeemA(TCMmCooperorgDealDTO cooperorgDealDTO, String frontTransNo) throws Exception{
		
		if(frontTransNo == null || "".equals(frontTransNo)){
    		frontTransNo = iInteractAssist.getInteractNo();   //获取前置接口交易流水号
    	}
		
		RedeemResultDto redeemResultDto = new RedeemResultDto();
		String retCode = ""; //返回码
		String retMsg = "";//返回message
		Long exceptionPrimaryKeyId; //事务异常表主键
		Map<String, String> resultMap = new HashMap<String, String>();
		TCMmTransConfigDTO transConfigDTO = new TCMmTransConfigDTO();
		Map<String, Object> searchParams = new HashMap<String, Object>();
		transConfigDTO.setTransEnName(CimsConstant.COOPERORG_TRANS_EN_NAME_T001);  //贷款回购交易发送核心整体事务
		transConfigDTO.setTransNo(CimsConstant.COOPERORG_TRANS_T001_A);
		searchParams.put("dto", transConfigDTO);
		List<TCMmTransConfigDTO>  tCMmTransConfigDTOList = transConfigService.searchTCMmTransConfig(searchParams);
		if(tCMmTransConfigDTOList != null && tCMmTransConfigDTOList.size() > 0){
			//得到事务配置信息
			TCMmTransConfigDTO dto = tCMmTransConfigDTOList.get(0);
			//前端处理1：贷款转出批量回购
			try {
				Map<String, Object> rsMap = this.doLoanTransferBatchRedeem(cooperorgDealDTO, frontTransNo);
				redeemResultDto = (RedeemResultDto)rsMap.get("dto");
				retCode = redeemResultDto.getRetCode();
				if (CimsConstant.RET_CODE_SUC.equals(redeemResultDto.getRetCode())) {
					retMsg = "贷款回购接口调用成功!";
					redeemResultDto.setRetMsg(retMsg);
				}
				redeemResultDto.setFrontTransNo(frontTransNo);
				exceptionPrimaryKeyId = transExceptionService.saveTransException(dto, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
			} catch (Exception e) {
				logger.error("贷款回购接口异常，异常信息如下："+e.getMessage());
				if (e.getMessage().indexOf(CimsExceptionConstant.INTERACT_NETWORK_ERROR) > -1) {
					retCode = CimsConstant.RET_CODE_TIMEOUT;  //超时
				} else {
					retCode = CimsConstant.RET_CODE_FAIL;
				}
				retMsg = "贷款回购接口调用失败："+ e.getMessage()+"!";
				retMsg = retMsg.length() > 100 ? retMsg.substring(0, 100) : retMsg;
				redeemResultDto.setRetCode(retCode);
				redeemResultDto.setRetMsg(retMsg);
				redeemResultDto.setFrontTransNo(frontTransNo);
				exceptionPrimaryKeyId = transExceptionService.saveTransException(dto, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
				/*result = this.doLoanTransferRedeemInterfaceTransException(cooperorgDealDTO, dto, retCode, exceptionPrimaryKeyId, i);*/
			}
		}else{
			retMsg = "贷款回购事务配置缺失!";
			retCode = CimsConstant.RET_CODE_FAIL;
		}
		
		resultMap.put("resultCode", retCode);
		resultMap.put("resultMsg", retMsg);
		return resultMap;
	}
	
	
	/**
	 * 贷款转出月还匹配债权赎回事务正常及异常处理
     * @throws Exception 
     * @Title doLoanTransferRedeemA
     * @Description TODO
     * @return 
     * @author yanjingying
     * @date 2015-12-23-下午11:47:00
     * @update
     * @throws
	 * */
	public Map<String, String> doLoanTransferRedeemB(TCMmCooperorgDealDTO cooperorgDealDTO, String frontTransNo) throws Exception{
		if(frontTransNo == null || "".equals(frontTransNo)){
    		frontTransNo = iInteractAssist.getInteractNo();   //获取前置接口交易流水号
    	}
		RedeemResultDto redeemResultDto = new RedeemResultDto();
		String retCode = ""; //返回码
		String retMsg = "";//返回message
		Long exceptionPrimaryKeyId; //事务异常表主键
		Map<String, String> resultMap = new HashMap<String, String>();
		TCMmTransConfigDTO transConfigDTO = new TCMmTransConfigDTO();
		Map<String, Object> searchParams = new HashMap<String, Object>();
		transConfigDTO.setTransEnName(CimsConstant.COOPERORG_TRANS_EN_NAME_T001);  //贷款回购交易发送核心整体事务
		transConfigDTO.setTransNo(CimsConstant.COOPERORG_TRANS_T001_B);
		searchParams.put("dto", transConfigDTO);
		List<TCMmTransConfigDTO>  tCMmTransConfigDTOList = transConfigService.searchTCMmTransConfig(searchParams);
		if(tCMmTransConfigDTOList != null && tCMmTransConfigDTOList.size() > 0){
			//得到事务配置信息
			TCMmTransConfigDTO dto = tCMmTransConfigDTOList.get(0);
			//前端处理1：贷款转出批量回购
			try {
				Map<String, Object> rsMap = this.doLoanTransferRePayMatchCreditRedeem(cooperorgDealDTO, frontTransNo);
				String isInvoke = (String)rsMap.get("isInvoke");
				if ("true".equals(isInvoke)) {
					redeemResultDto = (RedeemResultDto)rsMap.get("dto");
					retCode = redeemResultDto.getRetCode();
					if (CimsConstant.RET_CODE_SUC.equals(redeemResultDto.getRetCode())) {
						retMsg = "月还匹配债权赎回接口调用成功!";
						redeemResultDto.setRetMsg(retMsg);
					}
					redeemResultDto.setFrontTransNo(frontTransNo);
				} else {
					retCode = CimsConstant.RET_CODE_NO_INVOKE;
//					retMsg = "月还匹配债权赎回接口无需调用!";
				}
				exceptionPrimaryKeyId = transExceptionService.saveTransException(dto, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
			} catch (Exception e) {
				logger.error("月还匹配债权赎回异常，异常信息如下："+e.getMessage());
				if (e.getMessage().indexOf(CimsExceptionConstant.INTERACT_NETWORK_ERROR) > -1) {  
					retCode = CimsConstant.RET_CODE_TIMEOUT;   //超时
				} else {
					retCode = CimsConstant.RET_CODE_FAIL;
				}
				retMsg = "月还匹配债权赎回接口调用失败："+ e.getMessage()+"!";
				retMsg = retMsg.length() > 100 ? retMsg.substring(0, 100) : retMsg;
				redeemResultDto.setRetCode(retCode);
				redeemResultDto.setRetMsg(retMsg);
				redeemResultDto.setFrontTransNo(frontTransNo);
				exceptionPrimaryKeyId = transExceptionService.saveTransException(dto, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
				/*result = this.doLoanTransferRedeemInterfaceTransException(cooperorgDealDTO, dto, retCode, exceptionPrimaryKeyId, i);*/
			}
		}else{
			retMsg = "月还匹配债权赎回事务配置缺失!";
			retCode = CimsConstant.RET_CODE_FAIL;
		}
		resultMap.put("resultCode", retCode);
		resultMap.put("resultMsg", retMsg);
		return resultMap;
	}
	
	
	/**
	 * 贷款回购事务1:贷款转出批量回购处理
	 * 1. 主要将前端合作机构交易表的交易类型为贷款回购并且贷款明细流水表中符合条件信息封装成核心接口输入信息；
	 * 2. 调用核心接口后将贷款变更为我司贷款，将变更后的我司贷款进入我司债权池，调用会计API记会计账；
	 * 3. 变更【交易流水明细表】债权状态05-赎回；
	 * 4. 变更【债权明细表】债权状态05-赎回
	 * 其中合同编号=协议编号=债权编号
     * @throws Exception 
     * @Title doLoanTransferBatchRedeem
     * @Description TODO
     * @return 
     * @author yanjingying
     * @date 2015-12-17-下午11:47:00
     * @update
     * @throws
	 * */
	public Map<String, Object> doLoanTransferBatchRedeem(TCMmCooperorgDealDTO cooperorgDealDTO, String frontTransNo) throws Exception{
		Map<String, Object> rsMap = new HashMap<String, Object>();
		RedeemResultDto redeemResultDto = null;
		
		/**第1步：根据交易流水号获取交易流水明细表数据,过滤条件是:【核心债权状态为01-正常、95-提前结清、 86-贷款回购】且【债权来源为02-贷款】
		   此处以债权方式处理，可通过债权编号将【交易流水明细表】与【债权明细表】建立关联*/
		//1 获取交易流水明细表中回购贷款,并校验交易流水明细，若为空，则抛出异常，反之继续往下执行 
		Map<String, Object> creditDealSearchParams = new HashMap<String, Object>();
		String[] coreCreditStatus = new String[]{   
									CimsConstant.CORE_CREDIT_STATUS_LOANREDEEM, 
					                CimsConstant.CORE_CREDIT_STATUS_RED};
		TCMmCreditDealDetailDTO creditDealDetailDTO = new TCMmCreditDealDetailDTO();
		creditDealDetailDTO.setDealStreamCode(cooperorgDealDTO.getDealCode());    //交易流水号
		creditDealSearchParams.put("dto", creditDealDetailDTO);
		creditDealSearchParams.put("creditSource", CimsConstant.CREDIT_SOURCE_LOA);   //债权来源: 02-贷款
		creditDealSearchParams.put("coreCreditStatuss", coreCreditStatus);       //核心债权状态: 01-正常, 95-提前结清 
		List<TCMmCreditDealDetailDTO> creditDealDetailDTOList  = dealService.getDealDetailByCreditInfo(creditDealSearchParams);
		if(creditDealDetailDTOList == null || creditDealDetailDTOList.size() == 0){
			throw new Exception("合作机构交易表的交易流水号："+cooperorgDealDTO.getDealCode()+":在【交易流水表明细表】中找不到相关明细信息！");
		}
		
		//2 封装贷款回购的【贷款明细对象】，并校验【贷款明细对象】，若为空，则抛出异常，反之继续往下执行 
		List<TCMmCreditDetailDTO> newCreditDetailDTOList = new ArrayList<TCMmCreditDetailDTO>();
		for(TCMmCreditDealDetailDTO dto : creditDealDetailDTOList){
			TCMmCreditDetailDTO creditDetailDTO = new TCMmCreditDetailDTO();
			creditDetailDTO.setTransferId(dto.getTransferId());       //债权转让编号
			creditDetailDTO.setCreditCode(dto.getCreditorId());       //债权编号
			creditDetailDTO.setRedeemDate(dto.getRedeemDate());       //赎回日期
			creditDetailDTO.setCooperorgCode(dto.getCooperorgCode()); //合作机构代码
			creditDetailDTO.setThisRepaySum(dto.getThisRepaySum());   //月还金额
			creditDetailDTO.setCoreCreditStatus(dto.getCoreCreditStatus());  //核心债权状态
			creditDetailDTO.setProductType(dto.getProductType());//产品类型
			newCreditDetailDTOList.add(creditDetailDTO);              //贷款明细对象
		}
		if(newCreditDetailDTOList == null || newCreditDetailDTOList.size() == 0) {
			throw new Exception("合作机构交易表的交易流水号："+cooperorgDealDTO.getDealCode()+":封装的【贷款明细对象】为空！");
		}
		
		//3 封装贷款转出批量回购-回购参数DTO
		RedeemParamDto redeemParamDto = new RedeemParamDto();
		redeemParamDto.setDealCode(cooperorgDealDTO.getDealCode());  //交易流水号
		redeemParamDto.setCCode(cooperorgDealDTO.getCooperorgCode());  //合作机构代码
		redeemParamDto.settCMmCreditDetailDTOList(newCreditDetailDTOList);  //债权明细，此处债权方式处理
		redeemParamDto.setFrontTransNo(frontTransNo);//前置流水号
		
		//4 调用核心接口-贷款转出批量回购（说明：测试过程中测试接口时再调用）
		redeemResultDto = iCInteract.sendLoanTransferBatchRedeemToCore(redeemParamDto);
		
		//5 调用核心接口成功后，则更新债权状态05-赎回，赎回日期-当前确认日期
		if(redeemResultDto != null){
			String[] creditStatus = new String[]{   
					CimsConstant.CREDIT_STATUS_WIN, 
                    CimsConstant.CREDIT_STATUS_END};
			if(CimsConstant.RET_CODE_SUC.equals(redeemResultDto.getRetCode())){
				//更新 贷款数据【交易流水明细表】债权状态05-赎回, 更新时间
				dealService.updateTcmmCreditDealCreditorStatusByDealStreamCode(cooperorgDealDTO.getDealCode(), 
						CimsConstant.CREDIT_SOURCE_LOA, coreCreditStatus, creditStatus, CimsConstant.CREDIT_STATUS_RED);
				//更新【债权明细表】债权状态05-赎回, 赎回日期-当前确认日期
				dealService.updateTcmmCreditCreditorStatusByDealStreamCode(cooperorgDealDTO.getDealCode(), 
						CimsConstant.CREDIT_SOURCE_LOA, coreCreditStatus, creditStatus, CimsConstant.CREDIT_STATUS_RED);
			}
		}else{
			//暂时关闭测试
			throw new Exception("合作机构交易表的交易流水号："+cooperorgDealDTO.getDealCode()+":调用核心接口(贷款转出批量回购)返回结果为空！");
		}
		rsMap.put("dto", redeemResultDto);
		return rsMap;
	}
	
	/**
	 * 贷款回购事务2:贷款转出月还匹配债权赎回处理
	 * 1. 主要将贷款转出时月还匹配出剩余期数债权赎回，封装成核心接口输入信息；
	 * 2. 调用核心接口-转让债权批量赎回，将赎回该批次债权，调用会计API记会计账；
	 * 3. 单个事务调用成功后则更新债权明细表债权状态05-赎回、本期还款金额0、当前债权价值0；
     * @throws Exception 
     * @Title doLoanTransferRePayMatchCreditRedeem
     * @Description TODO
     * @return 
     * @author yanjingying
     * @date 2015-12-17-下午11:47:00
     * @update
     * @throws
	 * */
	public Map<String, Object> doLoanTransferRePayMatchCreditRedeem(TCMmCooperorgDealDTO cooperorgDealDTO, String frontTransNo)  throws Exception{
		Map<String, Object> rsMap = new HashMap<String, Object>();
		RedeemResultDto redeemResultDto = null;
		
		/**第1步：根据交易流水号获取交易流水明细表数据,过滤条件是:【核心债权状态为11-已确认转让  12-已赎回】且【债权来源为02-债权】
		   此处以债权方式处理，可通过债权编号将【交易流水明细表】与【债权明细表】建立关联*/
		//1 交易流水明细表中二次购买债权,并校验交易流水明细，若为空，则抛出异常，反之继续往下执行 
		Map<String, Object> creditDealSearchParams = new HashMap<String, Object>();
		String[] coreCreditStatus = new String[] {CimsConstant.CORE_CREDIT_STATUS_WIN, CimsConstant.CORE_CREDIT_STATUS_RED};
		TCMmCreditDealDetailDTO creditDealDetailDTO = new TCMmCreditDealDetailDTO();
		creditDealDetailDTO.setDealStreamCode(cooperorgDealDTO.getDealCode());    //交易流水号
		creditDealSearchParams.put("dto", creditDealDetailDTO);
		creditDealSearchParams.put("creditSource", CimsConstant.CREDIT_SOURCE_CRE);   //债权来源: 01-债权
		creditDealSearchParams.put("coreCreditStatuss", coreCreditStatus);       //核心债权状态: 10-已转让在途    11-已确认转让  12-已赎回
		List<TCMmCreditDealDetailDTO> creditDealDetailDTOList  = dealService.getDealDetailByCreditInfo(creditDealSearchParams);
		if(creditDealDetailDTOList == null || creditDealDetailDTOList.size() == 0){
			rsMap.put("isInvoke", "false");
			//throw new Exception("合作机构交易表的交易流水号："+cooperorgDealDTO.getDealCode()+":在【交易流水表明细表】中找不到相关明细信息！");
		}else{
			rsMap.put("isInvoke", "true");
			//2 封装二次购买债权的【债权明细对象】，若为空，则封装的【债权明细对象】为空，无需做赎回处理，反之，则需要做赎回处理
			List<CreditDetailDto> creditDetailList = new ArrayList<CreditDetailDto>();
			for(TCMmCreditDealDetailDTO newTCDTO : creditDealDetailDTOList){
				CreditDetailDto creditDetailDto = new CreditDetailDto();
				creditDetailDto.setTransferId(newTCDTO.getTransferId());   //债权转让编号
				creditDetailDto.setCreditCode(newTCDTO.getCreditorId());   //债权协议编号
				creditDetailDto.setIsWhole("Y");                           //是否全部赎回
				creditDetailDto.setThisCreditorValue(newTCDTO.getThisCreditorValue()); //本次赎回债权价值
				creditDetailDto.setRedeemDate(newTCDTO.getRedeemDate());   //赎回日期
				creditDetailList.add(creditDetailDto);                     //债权明细对象
			}
			
			if(creditDetailList != null && creditDetailList.size() > 0){
				//3 封装月还匹配债权批量赎回参数DTO
				RedeemParamDto redeemParamDto = new RedeemParamDto();    
				redeemParamDto.setDealCode(cooperorgDealDTO.getDealCode());   //交易流水号
				redeemParamDto.setCreditDetailList(creditDetailList);         //债权明细对象
				redeemParamDto.setFrontTransNo(frontTransNo);//前置流水号
				redeemParamDto.setCCode(cooperorgDealDTO.getCooperorgCode());//合作机构
				
				//4 调用核心接口-转让债权批量赎回（说明：测试过程中测试接口时再调用）
				redeemResultDto = iCInteract.redeemCreditRight(redeemParamDto);
				
				//5 调用核心接口成功后，则更新债权状态05-赎回，赎回日期-当前确认日期
				if(redeemResultDto != null){
					if(CimsConstant.RET_CODE_SUC.equals(redeemResultDto.getRetCode())){
						String[] creditStatus = new String[]{   
								CimsConstant.CREDIT_STATUS_WIN, 
		                        CimsConstant.CREDIT_STATUS_END};
						//更新 贷款数据【交易流水明细表】债权状态05-赎回, 更新时间 ,赎回日期-当前确认日期
						dealService.updateTcmmCreditDealCreditorStatusByDealStreamCode(cooperorgDealDTO.getDealCode(), 
								CimsConstant.CREDIT_SOURCE_LOA, coreCreditStatus, creditStatus, CimsConstant.CREDIT_STATUS_RED);
						//更新【债权明细表】债权状态05-赎回, 赎回日期-当前确认日期
						dealService.updateTcmmCreditCreditorStatusByDealStreamCode(cooperorgDealDTO.getDealCode(), 
								CimsConstant.CREDIT_SOURCE_LOA, coreCreditStatus, creditStatus, CimsConstant.CREDIT_STATUS_RED);
					}
				}else{
					//暂时关闭测试
					throw new Exception("合作机构交易表的交易流水号："+cooperorgDealDTO.getDealCode()+":调用核心接口(贷款转出月还匹配债权赎回)返回结果为空！");
				}
			}
		}
		rsMap.put("dto", redeemResultDto);
		return rsMap;
	}
	
	/**
	 * @throws Exception 
	 * 处理调用接口异常事务，必须保证多个事务同时成功
	 * 1. 若事务A失败，则重新调用事务A，直至成功为止
	 * 2. 若事务B失败，则重新调用事务B，直至成功为止
     * @throws Exception 
     * @Title doInterfaceForMoreTrans
     * @Description TODO
     * @return boolean
     * @author yanjingying
     * @date 2015-12-17-下午11:47:00
     * @update
     * @throws
	 * */
//	public boolean doLoanTransferRedeemInterfaceTransException(TCMmCooperorgDealDTO cooperorgDealDTO, TCMmTransConfigDTO transConfigDTO, String retCode, Long id, int count) throws Exception{
//		logger.info(DateUtil.getCurrentTime()+">>>>>>>>>>>>>>开始处理贷款回购接口事务异常>>>>>>>>>>>>>>");
//		RedeemResultDto redeemResultDto = null;
//		Long exceptionPrimaryKeyId; //事务异常表主键
//		boolean result = false;
//		System.out.println("=================================事务" + transConfigDTO.getTransNo() +":异常重复执行次数：" + count);
//		count ++;
//		if(count <= CimsConstant.COOPERORG_TRANS_MAX_EXEC_TIMES){  //异常重复执行次数不超过N次，N次以上不再执行，终止
//			if(CimsConstant.RET_CODE_FAIL.equals(retCode)){
//				//清除事务失败日志
//				//tCMmTransExceptionService.deleteTCMmTransExceptionByID(id);
//				
//				//若事务A失败，则重新调用事务A，直至成功为止
//				if("A".equals(transConfigDTO.getTransNo())){
//					try {
//						redeemResultDto = this.doLoanTransferBatchRedeem(cooperorgDealDTO);
//						retCode = CimsConstant.RET_CODE_SUC;
//						exceptionPrimaryKeyId = transExceptionService.saveTransException(transConfigDTO, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
//						result = true;
//					} catch (Exception e) {
//						result = false;
//						retCode = CimsConstant.RET_CODE_FAIL;
//						exceptionPrimaryKeyId = transExceptionService.saveTransException(transConfigDTO, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
//						this.doLoanTransferRedeemInterfaceTransException(cooperorgDealDTO, transConfigDTO, retCode, exceptionPrimaryKeyId, count);
//					}
//				}
//				
//				//若事务B失败，则重新调用事务B，直至成功为止
//				if("B".equals(transConfigDTO.getTransNo())){
//					try {
//						redeemResultDto = this.doLoanTransferRePayMatchCreditRedeem(cooperorgDealDTO);
//						retCode = CimsConstant.RET_CODE_SUC;
//						exceptionPrimaryKeyId = transExceptionService.saveTransException(transConfigDTO, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
//						result = true;
//					} catch (Exception e) {
//						result = false;
//						retCode = CimsConstant.RET_CODE_FAIL;
//						exceptionPrimaryKeyId = transExceptionService.saveTransException(transConfigDTO, redeemResultDto, retCode, cooperorgDealDTO.getDealCode());
//						this.doLoanTransferRedeemInterfaceTransException(cooperorgDealDTO, transConfigDTO, retCode, exceptionPrimaryKeyId, count);
//					}
//				}
//			}
//		}
//		logger.info(DateUtil.getCurrentTime()+">>>>>>>>>>>>>>处理贷款回购接口事务异常结束>>>>>>>>>>>>>>");
//		return result;
//	}
	
} 
